package com.panshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panshi.entity.AppEssayT;
import com.panshi.service.impl.AppEssayTServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 资讯表 前端控制器
 * </p>
 *
 * @author xhlMr
 * @since 2020-08-04
 */
@RestController
public class AppEssayTController {

    @Autowired
    private AppEssayTServiceImpl appEssayTService;


    @ApiOperation("查询所有咨询列表")
    @GetMapping("/app-essay-t/zxAll")
    public List<AppEssayT> zxAll(){
        return appEssayTService.list(null);
    }

    @ApiOperation("咨询表 按ID查询")
    @GetMapping("/app-essay-t/zxbyid/{id}")
    public AppEssayT zxbyid(@PathVariable Integer id){
        if(id==null){
            return new AppEssayT();
        }
        return appEssayTService.getById(id);
    }


    @ApiOperation("咨询指定分页查询页数与个数   page咨询页数 ，limit 多少  type排序")
    @GetMapping("/app-essay-t/zxPage/{page}/{limit}/{type}")
    public List<AppEssayT> zxPage( @PathVariable int page, @PathVariable int limit,@PathVariable String type){
        if (page < 1){
            page = 1;
        }
        // 查询这个分类下的所有问题，获取查询的数据信息
        Page<AppEssayT> pageParam = new Page<>(page, limit);
        QueryWrapper<AppEssayT> wr = new QueryWrapper<>();
        wr.orderByDesc(type,"ground_time");
        appEssayTService.page(pageParam, wr);
        List<AppEssayT> records = pageParam.getRecords();
        return records;
    }


    @ApiOperation("咨询分页查询  cid分类  page咨询页数 ，limit 多少  type排序")
    @GetMapping("/app-essay-t/zxCPage/{cid}/{page}/{limit}/{type}")
    public List<AppEssayT> zxCPage(
            @PathVariable("cid") int cid,
            @PathVariable("page") int page,
            @PathVariable("limit") int limit,
            @PathVariable("type") String type){
        if(page<1){
            page = 1;
        }

        // 查询这个分类下的所有问题，获取查询的数据信息
        Page<AppEssayT> pageParam = new Page<>(page, limit);
        if(cid==0){
            appEssayTService.page(pageParam, new QueryWrapper<AppEssayT>().orderByDesc(type));
        }else{
            appEssayTService.page(pageParam, new QueryWrapper<AppEssayT>().eq("classify_id",cid).orderByDesc(type));
        }

        List<AppEssayT> records = pageParam.getRecords();
        return records;
    }


    //乐观锁增加浏览量
    @GetMapping("/app-essay-t/essayReadAdd/{essayId}")
    public Boolean essayReadAdd(@PathVariable int essayId){
        AppEssayT byId = appEssayTService.getById(essayId);
        // todo: redis缓存. 防止阅读重复
        byId.setReadCount(byId.getReadCount()+1);
        return appEssayTService.updateById(byId);
    }

    //增加评论量
    @GetMapping("/app-essay-t/essayCommAdd/{essayId}")
    public Boolean essayCommAdd(@PathVariable int essayId){
        AppEssayT byId = appEssayTService.getById(essayId);
        byId.setCommentCount(byId.getCommentCount()+1);
        return appEssayTService.updateById(byId);
    }
}

